package com.anjubao.parking.municipal.parkinglot.service;

import com.anjubao.parking.municipal.common.global.BusinessCodeEnum;
import com.anjubao.parking.municipal.common.utils.BeanUtils;
import com.anjubao.parking.municipal.common.utils.MunicipalParkingUtils;
import com.anjubao.parking.municipal.common.utils.U;
import com.anjubao.parking.municipal.parkinglot.dto.ParkingGeomagneticResDto;
import com.anjubao.parking.municipal.parkinglot.dto.ParkingGeomagneticReturnDto;
import com.anjubao.parking.municipal.parkinglot.dto.ParkingGeomagneticSearchDto;
import com.anjubao.parking.municipal.parkinglot.dto.ParkingLotGeomagneticStatusDto;
import com.anjubao.parking.municipal.parkinglot.feign.ParkingLotFeignClient;
import com.anjubao.parking.municipal.parkinglot.form.ParkingGeomagneticForm;
import com.anjubao.parking.municipal.parkinglot.mapper.ParkingGeomagneticMapper;
import com.anjubao.parking.municipal.parkinglot.mapper.ParkingLotDeviceStatusMapper;
import com.anjubao.parking.municipal.parkinglot.model.ParkingLotDeviceGeomagneticSensor;
import com.anjubao.parking.municipal.parkinglot.model.ParkingLotDeviceStatus;
import com.anjubao.parking.municipal.parkinglot.util.FormTransfer;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * 地磁管理service
 * @author anjubao
 */
@Service
public class ParkingGeomagneticService extends ServiceImpl<ParkingGeomagneticMapper, ParkingLotDeviceGeomagneticSensor> {

    @Autowired
    private ParkingGeomagneticMapper parkingGeomagneticMapper;
    @Autowired
    private ParkingLotDeviceStatusMapper parkingLotDeviceStatusMapper;
    @Autowired
    private ParkingLotFeignClient parkingLotFeignClient;
    private static final String CODE="404";

    /**
     * 新增地磁信息
     * @param parkingGeomagneticForm
     */
    public ParkingGeomagneticReturnDto insertOne(ParkingGeomagneticForm parkingGeomagneticForm) {
        //判断该泊位是否已经添加地磁
        QueryWrapper<ParkingLotDeviceGeomagneticSensor> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("parking_lot_slot_code", parkingGeomagneticForm.getParkingLotSlotCode());
        ParkingLotDeviceGeomagneticSensor parkingLotDeviceGeomagneticSensor = parkingGeomagneticMapper.selectOne(queryWrapper);
        if (parkingLotDeviceGeomagneticSensor != null) {
            return null;
        }
        //判断机器码是否存在
        if (selectMachineNo(parkingGeomagneticForm)) {
            return new ParkingGeomagneticReturnDto(CODE);
        }
        //生成id和code
        Long id = U.id();
        String code = MunicipalParkingUtils.generateCode(id, BusinessCodeEnum.PARKING_GEOMAGNETIC_SENSOR_CODE.getCode());
        //根据泊位编号获取车场信息
        ParkingLotGeomagneticStatusDto parkingLotGeomagneticStatusDto = parkingLotFeignClient.getParkingLotByParkingLotSlotCode(parkingGeomagneticForm.getParkingLotSlotCode()).getResult();
        //将车场信息与地磁信息合并
        ParkingLotDeviceGeomagneticSensor parkingGeomagnetic = FormTransfer.from2ParkingGeomagnetic(parkingLotGeomagneticStatusDto, parkingGeomagneticForm);
        parkingGeomagnetic.setId(id);
        parkingGeomagnetic.setCode(code);
        //新增地磁
        parkingGeomagneticMapper.insert(parkingGeomagnetic);
        //封装设备状态数据
        ParkingLotDeviceStatus parkingLotDeviceStatus=FormTransfer.from2ParkingLotDeviceStatus(parkingLotGeomagneticStatusDto,parkingGeomagnetic);
        parkingLotDeviceStatus.setId(U.id());
        parkingLotDeviceStatus.setCode( MunicipalParkingUtils.generateCode(U.id(), BusinessCodeEnum.DEVICE_STATUS.getCode()));
        //新增地磁设备状态
        parkingLotDeviceStatusMapper.insert(parkingLotDeviceStatus);
        return new ParkingGeomagneticReturnDto(code);
    }

    /**
     * 编辑地磁信息
     * @param parkingGeomagneticForm
     */
    public ParkingGeomagneticReturnDto updateOne(ParkingGeomagneticForm parkingGeomagneticForm,String code) {
        //判断机器码是否存在
        if (selectMachineNo(parkingGeomagneticForm)) {
            return new ParkingGeomagneticReturnDto(CODE);
        }
        //根据泊位编号获取车场信息
        ParkingLotGeomagneticStatusDto parkingLotGeomagneticStatusDto = parkingLotFeignClient.getParkingLotByParkingLotSlotCode(parkingGeomagneticForm.getParkingLotSlotCode()).getResult();
        //将车场信息与地磁信息合并
        ParkingLotDeviceGeomagneticSensor parkingGeomagnetic = FormTransfer.from2ParkingGeomagnetic(parkingLotGeomagneticStatusDto, parkingGeomagneticForm);
        //更新地磁信息
        UpdateWrapper<ParkingLotDeviceGeomagneticSensor> updateGeomagnetic=new UpdateWrapper<>();
        updateGeomagnetic.eq("code",code);
        parkingGeomagneticMapper.update(parkingGeomagnetic, updateGeomagnetic);
        //封装设备状态数据
        ParkingLotDeviceStatus parkingLotDeviceStatus= FormTransfer.from2ParkingLotDeviceStatus(parkingLotGeomagneticStatusDto,parkingGeomagnetic);
        //更新地磁设备状态
        UpdateWrapper<ParkingLotDeviceStatus> updateStatus=new UpdateWrapper<>();
        updateStatus.eq("device_code",code);
        parkingLotDeviceStatusMapper.update(parkingLotDeviceStatus,updateStatus);
        return new ParkingGeomagneticReturnDto(code);
    }

    /**
     * 根据设备编号删除地磁信息
     * @param code
     * @return
     */
    public ParkingGeomagneticReturnDto removeOne(String code) {
        //构造删除地磁信息
        QueryWrapper<ParkingLotDeviceGeomagneticSensor> queryGeomagnetic=new QueryWrapper<>();
        queryGeomagnetic.eq("code",code);
        parkingGeomagneticMapper.delete(queryGeomagnetic);
        //构造删除地磁状态信息
        QueryWrapper<ParkingLotDeviceStatus> queryStatus=new QueryWrapper<>();
        queryStatus.eq("device_code",code);
        parkingLotDeviceStatusMapper.delete(queryStatus);
        return new ParkingGeomagneticReturnDto(code);
    }

    /**
     * 搜索地磁信息(分页)
     * @param parkingGeomagneticSearchDto
     * @param page
     * @param pageSize
     * @return
     */
    public Page<ParkingGeomagneticResDto> search(ParkingGeomagneticSearchDto parkingGeomagneticSearchDto,Integer page, Integer pageSize) {
        //初始化分页条件
        Page<ParkingGeomagneticResDto> formPage=new Page<>(page,pageSize);
        //分页查询地磁列表
        return formPage.setRecords(parkingGeomagneticMapper.search(parkingGeomagneticSearchDto,formPage));
    }

    /**
     * 根据设备编号查询地磁信息
     * @param code
     * @return
     */
    public ParkingGeomagneticForm selectOne(String code) {
        QueryWrapper<ParkingLotDeviceGeomagneticSensor> queryOne=new QueryWrapper<>();
        queryOne.eq("code",code);
        ParkingLotDeviceGeomagneticSensor parkingGeomagnetic = parkingGeomagneticMapper.selectOne(queryOne);
        return BeanUtils.transform(ParkingGeomagneticForm.class,parkingGeomagnetic);
    }

    /**
     * 更新地磁运行状态
     * @param isOnline
     * @param code
     * @return
     */
    public ParkingGeomagneticReturnDto updateIsOnline(Integer isOnline, String code) {
        UpdateWrapper<ParkingLotDeviceStatus> updateOnline= new UpdateWrapper<>();
        //更新条件
        updateOnline.eq("device_code",code);
        //更新内容
        ParkingLotDeviceStatus updateOne=new ParkingLotDeviceStatus();
        updateOne.setIsOnline(isOnline);
        parkingLotDeviceStatusMapper.update(updateOne, updateOnline);
        return new ParkingGeomagneticReturnDto(code);
    }

    /**
     *抽取判断机器码是否存在的方法
     * @param parkingGeomagneticForm
     * @return
     */
    private boolean selectMachineNo(ParkingGeomagneticForm parkingGeomagneticForm) {
        QueryWrapper<ParkingLotDeviceGeomagneticSensor> queryMachineNo=new QueryWrapper<>();
        queryMachineNo.eq("machine_no",parkingGeomagneticForm.getMachineNo());
        ParkingLotDeviceGeomagneticSensor parkingLotMachineNo = parkingGeomagneticMapper.selectOne(queryMachineNo);
        //机器码存在,判断该机器码是否是当前这条记录的
        if (parkingLotMachineNo!=null){
            if (!parkingLotMachineNo.getParkingLotSlotCode().equals(parkingGeomagneticForm.getParkingLotSlotCode())){
                return true;
            }
            return false;
        }
        return false;
    }

    /**
     * 根据车场code获取安装了地磁的泊位id列表
     * @param parkingLotCode
     * @return
     */
    public List<Long> selectGeomagnetic(String parkingLotCode) {
        return parkingGeomagneticMapper.selectGeomagnetic(parkingLotCode);
    }
}
